<?php

namespace App\Http\Controllers\Agency;

use App\Http\Controllers\Controller;
use App\Services\UserService;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;
use hg\apidoc\annotation as Apidoc;

/**
 * 用户信息
 * @Apidoc\Group("behind")
 */

class UserController extends Controller
{
    /**
     * @Apidoc\Title ("登录后获取用户信息")
     * @Apidoc\Url("/agency/userAuth")
     * @Apidoc\Method("GET")
     * @Apidoc\Header("Authorization", type="string",require=true, desc="Bearer + accessToken")
     * @Apidoc\Returned("username",type="string",desc="账号")
     * @Apidoc\Returned("mobile",type="string",desc="手机")
     * @Apidoc\Returned("avatar",type="string",desc="头像")
     * @Apidoc\Returned("weixin",type="string",desc="微信")
     * @Apidoc\Returned("wxcode",type="string",desc="微信二维码链接")
     * @Apidoc\Returned("roleid",type="int",desc="角色id")
     * @Apidoc\Returned("agencyid",type="int",desc="机构id")
     * @Apidoc\Returned("shortname",type="int",desc="机构简称")
     * @Apidoc\Returned("status",type="string",desc="状态：Y正常；N锁定")
     * @Apidoc\Returned("is_servicer",type="string",desc="是否客服：Y是；N否")
     * @Apidoc\Returned("is_super",type="string",desc="是否超管:Y是；N否")
     * @Apidoc\Returned("created_at",type="string",desc="创建时间")
     * @Apidoc\Returned("permissions",type="array",desc="权限数组")
     *
     */
    public function userAuth()
    {
        $user = JWTAuth::parseToken()->authenticate();
        $arr = json_decode($user->hasOneRole->permissions,true)['permissions'];
        $user->agencyname = $user->hasOneAgency->shortname;
        $user->permissions = $this->convertTree($arr);
        $user = $user->makeHidden(['hasOneRole','hasOneAgency']);
        return $this->responseSuccess($user);
    }

    /**
     * @Apidoc\Title ("用户列表不带分页")
     * @Apidoc\Url("/agency/getUserList")
     * @Apidoc\Method("POST")
     * @Apidoc\Header("Authorization", type="string",require=true, desc="Bearer + accessToken")
     * @Apidoc\Param("username",type="string",require=false,desc="账号")
     * @Apidoc\Param("realname",type="string",require=false,desc="姓名")
     * @Apidoc\Param("status",type="string",require=false,desc="账号状态")
     * @Apidoc\Param("is_service",type="string",require=false,desc="是否客服")
     * @Apidoc\Returned("username",type="string",desc="账号")
     * @Apidoc\Returned("mobile",type="string",desc="手机")
     * @Apidoc\Returned("avatar",type="string",desc="头像")
     * @Apidoc\Returned("weixin",type="string",desc="微信")
     * @Apidoc\Returned("wxcode",type="string",desc="微信二维码链接")
     * @Apidoc\Returned("roleid",type="int",desc="角色id")
     * @Apidoc\Returned("agencyid",type="int",desc="机构id")
     * @Apidoc\Returned("shortname",type="int",desc="机构简称")
     * @Apidoc\Returned("status",type="string",desc="状态：Y正常；N锁定")
     * @Apidoc\Returned("is_servicer",type="string",desc="是否客服：Y是；N否")
     * @Apidoc\Returned("is_super",type="string",desc="是否超管:Y是；N否")
     * @Apidoc\Returned("created_at",type="string",desc="创建时间")
     *
     */
    public function getUserList(UserService $userService , Request $request)
    {
        $params = $request->input();
        $params['aid'] = $this->getAid();
        $list = $userService->getUserList($params);
        return $this->responseSuccess($list);
    }

    /**
     * @Apidoc\Title ("用户列表带分页")
     * @Apidoc\Url("/agency/userList")
     * @Apidoc\Method("POST")
     * @Apidoc\Header("Authorization", type="string",require=true, desc="Bearer + accessToken")
     * @Apidoc\Param("page",type="int",require=false,desc="页数")
     * @Apidoc\Param("pageSize",type="int",require=false,desc="条数")
     * @Apidoc\Param("username",type="string",require=false,desc="账号")
     * @Apidoc\Param("realname",type="string",require=false,desc="姓名")
     * @Apidoc\Param("status",type="string",require=false,desc="账号状态")
     * @Apidoc\Param("is_service",type="string",require=false,desc="是否销售")
     * @Apidoc\Returned("username",type="string",desc="账号")
     * @Apidoc\Returned("mobile",type="string",desc="手机")
     * @Apidoc\Returned("avatar",type="string",desc="头像")
     * @Apidoc\Returned("weixin",type="string",desc="微信")
     * @Apidoc\Returned("wxcode",type="string",desc="微信二维码链接")
     * @Apidoc\Returned("roleid",type="int",desc="角色id")
     * @Apidoc\Returned("agencyid",type="int",desc="机构id")
     * @Apidoc\Returned("status",type="string",desc="状态：Y正常；N锁定")
     * @Apidoc\Returned("is_servicer",type="string",desc="是否销售：Y是；N否")
     * @Apidoc\Returned("is_super",type="string",desc="是否超管:Y是；N否")
     * @Apidoc\Returned("created_at",type="string",desc="创建时间")
     *
     */
    public function userList(UserService $userService , Request $request)
    {
        $params = $request->input();
        $params['aid'] = $this->getAid();
        $list = $userService->getList($params);
        return $this->responseSuccess( $list);
    }
    /**
     * @Apidoc\Title ("保存用户")
     * @Apidoc\Url("/agency/userAdd")
     * @Apidoc\Method("POST")
     * @Apidoc\Header("Authorization", type="string",require=true, desc="Bearer + accessToken")
     * @Apidoc\Param("id",type="string",desc="新增时传空，修改是传列表id")
     * @Apidoc\Param("username",type="string",desc="账号")
     * @Apidoc\Param("mobile",type="string",desc="手机")
     * @Apidoc\Param("avatar",type="string",desc="头像")
     * @Apidoc\Param("weixin",type="string",desc="微信")
     * @Apidoc\Param("wxcode",type="string",desc="微信二维码链接")
     * @Apidoc\Param("roleid",type="int",desc="角色id")
     * @Apidoc\Param("agencyid",type="int",desc="机构id")
     * @Apidoc\Param("status",type="string",desc="状态：Y正常；N锁定")
     * @Apidoc\Param("is_servicer",type="string",desc="是否销售：Y是；N否")
     * @Apidoc\Param("is_super",type="string",desc="是否超管:Y是；N否")
     * @Apidoc\Returned("true",type="bool",desc="保存状态")
     *
     */
    public function userAdd(UserService $userService , Request $request)
    {
        $params = $request->input();
        $params['aid'] = $this->getAid();
        $result = $userService->addUser($params);
        if($result['status'])
            return $this->responseSuccess($result['status']);
        else
            return $this->responseError($result['code']);
    }

    /**
     * @Apidoc\Title ("删除用户")
     * @Apidoc\Url("/agency/delUser")
     * @Apidoc\Method("POST")
     * @Apidoc\Header("Authorization", type="string",require=true, desc="Bearer + accessToken")
     * @Apidoc\Param("ids",type="int",require=false,desc="列表id  1,2,3,4,....")
     * @Apidoc\Returned("true",type="bool",desc="保存状态")
     *
     */
    public function delUser(UserService $userService , Request $request)
    {
        $params = $request->input();
        $result = $userService->delUser($params);
        if($result)
            return $this->responseSuccess($result);
        else
            return $this->responseError(400);
    }
}
